﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using DBUtility;
using System.Text;

public partial class Promotion_Promotion7Edit : System.Web.UI.Page
{
    protected void Page_PreInit(object sender, EventArgs e)
    {
        Page.Theme = BLL.Common.GetWSMThemes();
    }

    private string Code
    {
        get
        {
            if (Request.QueryString["code"] != null)
            {
                return Request.QueryString["code"].ToString();
            }
            else
            {
                return "";
            }
        }
    }

    private Model.CXFAD7 Property
    {
        get
        {
            Model.CXFAD7 property = null;
            if (ViewState["cxfad7"] != null)
            {
                property = (Model.CXFAD7)ViewState["cxfad7"];
            }
            return property;
        }
        set
        {
            ViewState["cxfad7"] = value;
        }
    }
    private bool IsAdd
    {
        get
        {
            return Convert.ToBoolean(ViewState["IsAdd"]);
        }
        set
        {
            ViewState["IsAdd"] = value;
        }

    }
    private int Status
    {
        get
        {
            if (Request.QueryString["status"] != null)
            {
                return int.Parse(Request.QueryString["status"]);
            }
            else
            {
                return 0;
            }
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (this.Code == "")
            {

                this.lbl_Promotion.Text = BLL.Common.GetDM("300200101");
                BLL.Common.ADDLSH("300200101");
                string start = DateTime.Now.Year.ToString() + "-" + DateTime.Now.Month.ToString() + "-"
                            + DateTime.Now.Day.ToString();
                string end = (DateTime.Now.Month.ToString() == "12" ? Convert.ToString(Convert.ToInt32(DateTime.Now.Year) + 1) : DateTime.Now.Year.ToString()) + "-" + (DateTime.Now.Month.ToString() == "12" ? "01" : Convert.ToString(Convert.ToInt32(DateTime.Now.Month) + 1)) + "-" + (DateTime.Now.Day.ToString());
                this.Text1.Value = start;
                this.Text2.Value = end;

                this.IsAdd = true;
                btnAddOrUpdate.Text = "添加";
            }
            else
            {
                idTitle.InnerText = "修改满X送X元";
                Model.CXFAD7 cxfad7 = new BLL.CXFAD7().GetModel(this.Code);
                lbl_Promotion.Text = cxfad7.DJBH;
                txt_ActiveName.Text = cxfad7.HDMC;
                this.Text1.Value = cxfad7.RQ_S.ToShortDateString();
                this.Text2.Value = cxfad7.RQ_E.ToShortDateString();
                txt_MMJE.Text = cxfad7.BYZD5.ToString("0.00");
                txt_ZSJE.Text = cxfad7.BYZD6.ToString("0.00");
                Tbx_Remark.Value = cxfad7.BZ;
                drp_Type.SelectedValue = cxfad7.BYZD1.ToString();
                this.IsAdd = false;
                btnAddOrUpdate.Text = "更新";

                if (this.Status == 2)
                {
                    btnAddOrUpdate.Visible = true;
                }
                else
                {
                    btnAddOrUpdate.Visible = false;
                }
            } 
            
        }
    }

    protected void btnAddOrUpdate_Click(object sender, EventArgs e)
    {
        if (this.txt_ActiveName.Text.Trim() == string.Empty)
        { Share.Alert(this, "活动名称不能为空!"); return; }

        try
        {
            if (this.IsAdd)
            {
                Model.CXFAD7 cxfad7 = new Model.CXFAD7();
                cxfad7.DJBH = lbl_Promotion.Text;
                cxfad7.RQ = DateTime.Now;
                cxfad7.HDMC = txt_ActiveName.Text;
                cxfad7.RQ_S = DateTime.Parse(Text1.Value);
                cxfad7.RQ_E = DateTime.Parse(Text2.Value);
                cxfad7.BYZD1 = int.Parse(drp_Type.SelectedValue);
                Decimal result;
                cxfad7.BYZD5 = Decimal.TryParse(txt_MMJE.Text, out result) ? result : 0.00M;
                cxfad7.BYZD6 = Decimal.TryParse(txt_ZSJE.Text, out result) ? result : 0.00M;
                cxfad7.BZ = Tbx_Remark.Value;
                cxfad7.JZ = "0";
                cxfad7.SP = "0";
                cxfad7.RUN = "0";
                cxfad7.BYZD2 = 2;//初始 默认为未审批状态
                if (new BLL.CXFAD7().Add(cxfad7).Length > 0)
                {
                    //Share.Alert(this, "添加成功");
                }
            }
            else
            {
                Model.CXFAD7 cxfad7 = new BLL.CXFAD7().GetModel(this.Code);
                cxfad7.HDMC = txt_ActiveName.Text;
                cxfad7.RQ_S = DateTime.Parse(Text1.Value);
                cxfad7.RQ_E = DateTime.Parse(Text2.Value);
                cxfad7.BYZD1 = int.Parse(drp_Type.SelectedValue);
                Decimal result;
                cxfad7.BYZD5 = Decimal.TryParse(txt_MMJE.Text, out result) ? result : 0.00M;
                cxfad7.BYZD6 = Decimal.TryParse(txt_ZSJE.Text, out result) ? result : 0.00M;

                cxfad7.BZ = this.Tbx_Remark.Value;
                if (new BLL.CXFAD7().Update(cxfad7))
                {
                    //Share.Alert(this, "更新成功");
                }
            }
        }
        catch (Exception ee)
        {
            Share.Alert(this, "操作失败"); return;
        }

        Response.Redirect("Promotion7List.aspx", true);



    }
    
}
